package com.example.drouce.json_parse;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";


    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.json_text);
        String jsonData = getJSONString();
        JsonParse jsonParse = new JsonParse();
        List<Person> list = jsonParse.parseJSON(jsonData);

        for (Person peson : list){
            Log.d(TAG,peson.getNames() +"\t" + peson.getAge() +"\t"+ peson.getSex() +"\t"+ peson.getClasses());
        }

    }


    /* 获得要解析的JSON字符串*/
    public String getJSONString () {
        String jsonString = null;
        try {
            //打开assets下的文件
            InputStream inputStream = getAssets().open("Personclass.json");
            //用BufferReader读取文件流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            //StringBuilder 来拼接字符串
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);//每读一行，拼接一行
            }
            jsonString = stringBuilder.toString();
            //textView.setText(stringBuilder.toString());
            Log.d("获得的JSON字符串",stringBuilder.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonString;
    }


}
